<?php
/**
 * Created by PhpStorm.
 * User: zq
 * Date: 2017/2/27
 * Time: 15:03
 */

namespace Admin\Controller;


use Common\Controller\AdminController;
use Common\Model\GoodsAttrModel;
use Common\Model\GoodsListModel;
use Common\Model\TypeAttrModel;

class GoodsListController extends AdminController
{
    protected $ggid;

    public function index()
    {
        $gid = I('get.gid');
        $this->ggid = $gid;
//        p($gid);die;
        $data = (new TypeAttrModel())->alias('ta')
            ->join("sh_goods_attr ga on ta.aid=ga.aid")
            ->where("class=1 and ga.gid=$gid")->field('ga.aid,aname,gtid,gtvalue')->select();
//        p($data);
        $temp = [];
        foreach ($data as $k => $v) {
            $temp[$v['aid']]['name'] = $v['aname'];
            $temp[$v['aid']]['value'][] = $v;
        }
//        p($temp);
        $this->assign('temp', $temp);
        if (IS_POST) {
//            p($_POST);
            $data = I('post.');
            $data['combine'] = implode(',', $data['combine']);
//            p($data);die;
//            p($data['combine']);
            $data['gid'] = $gid;
            $this->store(new GoodsListModel(), $data, function () {
                $this->success('操作成功', u('GoodsList/index', ['gid' => $this->ggid]));
                die;
            });;
        }
        $oldData = (new GoodsListModel())->select();
//        p($oldData);die;
        foreach ($oldData as $k => $v) {
            $oldData[$k]['combine'] = explode(',', $v['combine']);
        }
//        p($oldData);
        foreach ($oldData as $k => $v) {
//            p($v);
            foreach ($v['combine'] as $kk => $vv) {

                $oldData[$k]['combine'][$kk] = (new GoodsAttrModel())->where("gtid='{$vv}'")
                    ->getField('gtvalue');
            }
        }
//        p($oldData);
        $this->assign('oldData', $oldData);
        $this->display();
    }

    //编辑
//    public function edit(){
//        $glid = I('get.glid');
//        $oldData = m("goods_list")->where("glid=$glid")->select();
////        p($oldData);
//        $this->assign('oldData',$oldData);
//        $this->display();
//    }
    //删除
    public function del()
    {
        $glid = I('get.glid');
        $model = new GoodsListModel();
        if ($model->where("glid={$glid}")->delete()) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }

}